package com.maibow.util;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.springframework.web.servlet.view.document.AbstractExcelView;

/**
 * 导入到EXCEL
 * 脉博网络后台管理系统
 * 包名：com.maibow.util
 * 文件名：ObjectExcelView.java
 * 作者: 吴比(Wubi)
 * 时间：2015年4月2日-上午9:15:21
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
public class ObjectExcelView extends AbstractExcelView {

    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        Date date = new Date();
        String filename = Tools.date2Str(date, "yyyyMMddHHmmss");
        HSSFSheet sheet;
        HSSFCell cell;
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
        sheet = workbook.createSheet("sheet1");

        List<String> heads = (List<String>) model.get("heads");
        if (heads != null && heads.size() > 0) {
            List<String> titles = (List<String>) model.get("titles");
            int len = titles.size();
            HSSFCellStyle headerStyle = workbook.createCellStyle(); // 标题样式
            HSSFCellStyle contentStyle = workbook.createCellStyle(); // 内容样式

            headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont headerFont = workbook.createFont(); // 标题字体
            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            headerFont.setFontHeightInPoints((short) 11);
            headerStyle.setFont(headerFont);
            short width = 20, height = 25 * 20;
            sheet.setDefaultColumnWidth(width);

            // 头部两行
            // 第一行
            String head1 = heads.get(0);
            cell = getCell(sheet, 0, 1);
            cell.setCellStyle(headerStyle);
            setText(cell, head1);

            if (heads.size() > 1) {
                // 第二行
                String head2 = heads.get(1);
                cell = getCell(sheet, 1, 2);
                cell.setCellStyle(contentStyle);
                setText(cell, head2);
            }

            // 标题
            for (int i = 0; i < len; i++) { // 设置标题
                String title = titles.get(i);
                cell = getCell(sheet, 2, i);
                cell.setCellStyle(headerStyle);
                setText(cell, title);
            }
            sheet.getRow(0).setHeight(height);
            contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            List<PageData> varList = (List<PageData>) model.get("varList");
            int varCount = varList.size();
            for (int i = 0; i < varCount; i++) {
                PageData vpd = varList.get(i);
                for (int j = 0; j < len; j++) {
                    String varstr = vpd.get("var" + (j + 1)) != null ? vpd.get("var" + (j + 1)).toString() : "";
                    cell = getCell(sheet, i + 3, j);
                    cell.setCellStyle(contentStyle);
                    setText(cell, varstr);
                }
            }
        } else {
            List<String> titles = (List<String>) model.get("titles");
            int len = titles.size();
            HSSFCellStyle headerStyle = workbook.createCellStyle(); // 标题样式
            headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont headerFont = workbook.createFont(); // 标题字体
            headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            headerFont.setFontHeightInPoints((short) 11);
            headerStyle.setFont(headerFont);
            short width = 20, height = 25 * 20;
            sheet.setDefaultColumnWidth(width);
            for (int i = 0; i < len; i++) { // 设置标题
                String title = titles.get(i);
                cell = getCell(sheet, 0, i);
                cell.setCellStyle(headerStyle);
                setText(cell, title);
            }
            sheet.getRow(0).setHeight(height);

            HSSFCellStyle contentStyle = workbook.createCellStyle(); // 内容样式
            contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            List<PageData> varList = (List<PageData>) model.get("varList");
            int varCount = varList.size();
            for (int i = 0; i < varCount; i++) {
                PageData vpd = varList.get(i);
                for (int j = 0; j < len; j++) {
                    String varstr = vpd.getString("var" + (j + 1)) != null ? vpd.getString("var" + (j + 1)) : "";
                    cell = getCell(sheet, i + 1, j);
                    cell.setCellStyle(contentStyle);
                    setText(cell, varstr);
                }

            }
        }
    }
}